// pages/mgr/paging.js
// components/paging/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    arrayList: Array, //表格数据
    titleList: Array, //表头数据
    currentIndex: { //当前页码
      type: Number,
      value: 1
    },
    totalPage: {
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    index: 1,
    total: 10,
    pageMask: false,
    prevBtnDis: true,
    nextBtnDis: true,
    dataList: [],
    titleList: []
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    // 在组件实例进入页面节点树时执行
    attached: function () {
      this.setData({
        index: this.data.currentIndex,
        total: this.data.totalPage,
        dataList: this.data.arrayList,
        titleList: this.data.titleList
      })
    },
    // 在组件实例被从页面节点树移除时执行
    detached: function () {

    }
  },
  methods: {
    flushData: function () {
      this.setData({
        dataList: this.data.arrayList,
        index: this.data.currentIndex,
        total: this.data.totalPage
      })
      this.judgeBtnDis();
    },
    //每次改变页码就调用该函数
    currentChangeEmit: function (touchState) {
      // 自定义组件向父组件传值 
      const option = {
        currentIndex: this.data.index,
        touchState: true
      };
      if (touchState) option.touchState = !touchState;
      // pagingChange 自定义名称事件，父组件中使用
      this.triggerEvent('pagingChange', option)
      /*
       在父组件中写上bind:pagingChanget="get_emit",在父组件中就需要调用get_emit事件
      */
    },
    //开启页码弹窗
    shopPagePopup: function () {
      this.setData({
        pageMask: true
      })
    },
    //关闭页码弹窗
    hidePagePopup: function () {
      this.setData({
        pageMask: false
      })
    },
    //更改页码点击事件
    changePage: function (e) {
      this.setData({
        pageMask: false,
        index: e.currentTarget.dataset.index
      })
      if (this.data.prevBtnDis || this.data.nextBtnDis) {
        this.currentChangeEmit(true);
      } else {
        this.currentChangeEmit();
      }
      this.judgeBtnDis();
    },
    //上一页点击事件
    prevPage: function () {
      if (this.data.prevBtnDis) {
        return
      }
      let num = (this.data.index == 1) ? 1 : this.data.index - 1;
      this.setData({
        index: num
      })
      if (this.data.prevBtnDis) {
        this.currentChangeEmit(true);
      } else {
        this.currentChangeEmit();
      }
      this.judgeBtnDis();
    },
    //下一页点击事件
    nextPage: function () {
      if (this.data.nextBtnDis) {
        return
      }
      let num = (this.data.index == this.data.total) ? this.data.total : this.data.index + 1;
      this.setData({
        index: num
      })
      if (this.data.nextBtnDis) {
        this.currentChangeEmit(true);
      } else {
        this.currentChangeEmit();
      }
      this.judgeBtnDis();
    },
    //判断按钮是否为disabled
    judgeBtnDis: function () {
      let index = this.data.index;
      if (index >= this.data.total) {
        this.setData({
          nextBtnDis: true
        })
      } else {
        this.setData({
          nextBtnDis: false
        })
      }

      if (index > 1) {
        this.setData({
          prevBtnDis: false
        })
      } else {
        this.setData({
          prevBtnDis: true
        })
      }

    },
    editItem(e) {
      const option = {
        selectIndex: e.currentTarget.dataset.index
      };
      this.triggerEvent('editItem', option)
    },
    removeItem(e) {
      const option = {
        selectIndex: e.currentTarget.dataset.index
      };
      this.triggerEvent('removeItem', option)
    },detailItem(e) {
      const option = {
        selectIndex: e.currentTarget.dataset.index,
        buttonIndex: e.currentTarget.dataset.bindex
      };
      this.triggerEvent('detailItem', option)
    }
  }
})